package fr.uniscol.model.valid;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import fr.uniscol.model.DefaultUser;
import fr.uniscol.model.IUniscolUser;

public class LoginFormModel implements Validator {

    private IUniscolUser userModel;
	
    private String password = "";
	
    public LoginFormModel(){
            this.userModel = new DefaultUser();
    }

    public IUniscolUser getUserModel() {
            return userModel;
    }

    public void setUserModel(IUniscolUser userModel) {
            this.userModel = userModel;
    }

    public String getLogin(){
            return userModel.getLogin();
    }

    public void setLogin(String login){
            ((DefaultUser)this.userModel).setLogin(login);
    }

    public String getPassword() {
            return password;
    }

    public void setPassword(String password) {
            this.password = password;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return LoginFormModel.class.equals(clazz);
    }

    @Override
    public void validate(Object obj, Errors err) {
        LoginFormModel model = (LoginFormModel) obj;

        if(model.getLogin().length() < 3){
            err.rejectValue("login", "login.error", "Le login doit faire plus de 3 caractères !");
        }else{
            System.out.println("OK !");
        }
    }
}
